﻿package com.vicentediaz.data
{
    import com.vicentediaz.data.events.*;
    import flash.display.*;
    import flash.events.*;
    import flash.net.*;
    import flash.utils.*;

    public class Gallery extends EventDispatcher
    {
        private var _totalThumbs:uint;
        private var nextThumb:int;
        private var verbose:Boolean = true;
        private var bitmaps:Array;
        private var loadersIndex:Dictionary;
        private var _xml:XML;
        private var filesIndex:Dictionary;
        private var allLoadedDispatched:Boolean;
        private static var instance:Gallery;

        public function Gallery(param1:IEventDispatcher = null)
        {
            super(param1);
            return;
        }// end function

        public function get totalThumbs() : uint
        {
            return this._xml.image.length();
        }// end function

        public function loadThumbs(param1:XML) : void
        {
            this._xml = param1;
            this._totalThumbs = param1.image.length();
            this.filesIndex = new Dictionary();
            this.loadersIndex = new Dictionary();
            this.bitmaps = new Array(this._xml.image.length());
            var _loc_2:* = Number(Config.getInstance().getByValue("SIMULTANEOUS_DOWNLOADS"));
            _loc_2 = _loc_2 < this._totalThumbs ? (_loc_2) : (this._totalThumbs);
            this.allLoadedDispatched = false;
            this.nextThumb = 0;
            var _loc_3:uint;
            while (_loc_3++ < _loc_2)
            {
                // label
                this.loadNextThumb();
            }// end while
            var _loc_4:uint;
            while (_loc_4++ < this._xml.image.length())
            {
                // label
                this.filesIndex[this._xml.image[_loc_4].toString()] = _loc_4;
            }// end while
            dispatchEvent(new GalleryEvent(GalleryEvent.START_TO_LOAD));
            return;
        }// end function

        private function say(param1:String) : void
        {
            if (this.verbose)
            {
                trace("<< GALLERY : " + param1 + " >>");
            }// end if
            return;
        }// end function

        public function get loadedThumbs() : uint
        {
            return this.nextThumb;
        }// end function

        private function onThumbLoaded(param1:Event) : void
        {
            var _loc_4:GalleryEvent;
            var _loc_2:* = new GalleryEvent(GalleryEvent.NEXT_THUMB_LOADED);
            _loc_2.totalThumbs = this._totalThumbs;
            _loc_2.currentThumbs = this.nextThumb;
            dispatchEvent(_loc_2);
            var _loc_3:* = LoaderInfo(param1.target).loader;
            this.bitmaps[this.filesIndex[this.loadersIndex[_loc_3]]] = Bitmap(_loc_3.content).bitmapData;
            this.say("Asociando " + this.loadersIndex[_loc_3] + " a " + this.filesIndex[this.loadersIndex[_loc_3]]);
            _loc_3.unload();
            if (this.nextThumb < this._xml.image.length())
            {
                this.loadNextThumb();
            }
            else if (!this.allLoadedDispatched)
            {
                _loc_4 = new GalleryEvent(GalleryEvent.ALL_THUMBS_LOADED);
                _loc_4.totalThumbs = this._totalThumbs;
                this.allLoadedDispatched = true;
                dispatchEvent(_loc_4);
            }// end else if
            return;
        }// end function

        public function getThumbBitmapData(param1:uint) : BitmapData
        {
            return this.bitmaps[param1];
        }// end function

        private function loadNextThumb() : void
        {
            var _loc_1:* = new Loader();
            var _loc_2:* = Config.getInstance().getByValue("THUMBS_PATH") + this._xml.image[this.nextThumb];
            _loc_1.load(new URLRequest(_loc_2));
            this.say("Cargando " + _loc_2);
            _loc_1.contentLoaderInfo.addEventListener(Event.COMPLETE, this.onThumbLoaded, false, 0, true);
            this.loadersIndex[_loc_1] = this._xml.image[this.nextThumb].toString();
            var _loc_3:String;
            _loc_3.nextThumb = this.nextThumb++;
            return;
        }// end function

        public static function getInstance() : Gallery
        {
            instance = instance ? (instance) : (new Gallery);
            return instance;
        }// end function

    }
}
